/// Serde是一个用于高效和通用地序列化和反序列化Rust数据结构的框架。

use serde_json::{Map, Number};
use serde::{Deserialize, Serialize};

// 要想使用序列化注释必须将依赖这样声明 serde = { version = "1.0.188", features = ["derive"] }
#[derive(Serialize, Deserialize, Debug)]
struct User {
    name: String,
    age: u32,
    phone: String,
}

enum Value {
    Null,
    Bool(bool),
    Number(Number),
    String(String),
    Array(Vec<Value>),
    Object(Map<String, Value>),
}

fn main() {
    let user = User {
        name: String::from("龙茶清欢"),
        age: 20,
        phone: String::from("13647933851")
    };

    // 将结构体转换成 json 字符串，序列化
    let serialized = serde_json::to_string(&user).unwrap();
    println!("序列化结果: {}", serialized);
    // 格式化打印，更加美观
    let serialized = serde_json::to_string_pretty(&user).unwrap();
    // 打印 序列化结果: {"name":"龙茶清欢","age":20,"phone":"13647933851"}
    println!("序列化结果: {}", serialized);

    // 将 json 字符串转换成结构体，反序列化
    let deserialized: User = serde_json::from_str(&serialized).unwrap();

    // 打印 反序列化结果: User { name: "龙茶清欢", age: 20, phone: "13647933851" }
    println!("反序列化结果: {:?}", deserialized);
}